package com.yz.xyt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.Arrays;


/**
 * @author single
 */
@Component
@Aspect
@EnableAspectJAutoProxy
public class ControllerAdvice {

    private Logger logger = LoggerFactory.getLogger("ControllerLog");

    /**
     * 日志记录
     * 切人controller包下面所有的public 方法
     *
     * @param point 切点
     */
    @Around("execution( public * com.yz.xyt.controller..*.*(..) )")
    public Object logMethod(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        logger.info(Arrays.toString(args));
        Object proceed = null;
        try {
            proceed = point.proceed();
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            throw e;
        }

        if (proceed == null) {
            logger.info("null");
        }else {
            logger.info(proceed.toString());
        }
        return proceed;
    }

}
